{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## I. Broadcasting\n",
    "----\n",
    "\n",
    "Xem kỹ về [Broadcasting](https://docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://lh3.googleusercontent.com/0N1zOMYdCjlL8IoM1nDnCxR69AAOrfnZ5U1mITWgsPaQwpiFYX9qJnCgyI6VUIDmNuQ7N11ONplmZKaEvaRj2ZQ8IrtsP87rQmKrZ2_9f2JJU2plT1OA1W5jprfgiVB6kRSkb77zI1KA5VDMQ5hbdXDEhkB3AjGUqak2syWSg4kNVez7jIVQQlpcz6oYIP0Mg4ZyioY14YDbTl_8XpgEg4jP98IaLgelXLVqBYy8A1AmoVRyXF6d9DkJRZJG_w4sdx9L5V6na0P8ocEzd6qFm1l7zhaCm-aXWUh6hpQZDUNsGVzuLfziWoB0qPsecv56EWBJJ3CpSK-RH2d5lmQnL88r_JXwQki45hJrOCamVVOb71acp0uJoMaMpFvEkyDNNLi3sNsSHCUkvg-RB58KUenFohtGGnwsWYZjOSJW7j1tZ1KNRIrWmCj0p7w85ofcEwg_wgTERVsCz4HK8T1rUDr6HMPyRB7o1UBrmCx3Gz9YCLTwqutPmieMaevMgADEVGA3k-DeQS67_wVVuhrYbW4EMNgQ4aSSpYYE3IwrvTQ3OkjMqHvOUy6Jm-MLIIt-_xdph28LBHdVMeVw0pFRx0RafVgT4RGdN6AqmSICaBQHhV8GUMCAHCwtZfySfbp6Gkf5lILoO_nqkezIo-Ww8sWjbskCsoqWFg=w1200-h800-no)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Boardcasting chúng ta có thể dịch là **lan truyền,** cách mà chúng ta thay đổi đồng loạt giá trị nhiều phần tử của mảng. \n",
    "\n",
    "Cụ thể hơn nữa Numpy cho phép thực hiện tính toán giữa các mảng với số và các mảng khác chiều với nhau."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Broadcasting](https://lh3.googleusercontent.com/vCzozuJR8Ljkc15pJmSLzK-YCEDTlhbPXjMuua--dqQ54Choz2HvN9HVogbv5mf70ZnqJ1fByiNupSL5_o9EMi8WNLxVVq1DiW9EGfKEdhv876Q6yFQreM0R0eu9LwI1vbGaOUpPCg_5C9_YB7iTkcVGwRyG5IXJ7Hk6DR6NjZxxHH64rNrgcjc6zapW1raWe3uhXuVH4pd6yhmhGcziutj-as5kK0l1XbaSigv5Tr7PokwKqaOspZKYM4y3tHMDbOCYtnLMySseiXR_eFLQsZbUc7d9m9uFPYgG1rQkZCxXn9tlpow29IBhp-3osA6quLzGIfcsYVpHr0xCwEj4gYsYyYY1-96WQdW0-cKYQlpLH13q1Cqd4E3WASrySVxAbqcTOl-E03k3SB2mWn5pvcK8f-Wzk108uL5bhGpmvvDAEOCJP9o41CCn8E8eYvGaDOgAjvYudtGEZVHPn4oyQPY9GRXv85pXkP9qGuFiKUVPnr4cnoc9w-Sy-dTHw_PSLaleyMm6w-m65h8XgnsBy5BwKG6ERs6A1kJVDfUIYC1SVQipoK7wotAgzBkU0JMQeH_NknmHWnPRJV9aZdJ7DcQ9HLsQRLTTrEktGgzqIbIjNlSxipZsQj_KRu5tLAFzUxIyLdczF2OFqlTCWUd6IK6_tm2wycoZrg=w432-h324-no)\n",
    "\n",
    "Hình 2: Nguồn [Python Data Science Handbook - Jake VanderPlas](https://jakevdp.github.io/PythonDataScienceHandbook/02.05-computation-on-arrays-broadcasting.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### I. Lan truyền mảng với số"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 1. Phép cộng mảng với số"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### a. Mảng một chiều"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://lh3.googleusercontent.com/suxWObwr6sEuDv_FT6hr2ht_tDcbDwrTLgD94h6fSXtSJe6-DFUZOR0yllqVyGHiUYCSvw-62ogO5XnMqH21_Z1JUTA4JvvQEuL68NyveaNKPQo1faNDCevAm0z7AwrbaqKk9Wdp_e5qBPicxtYPHfl_04D4A6AbChhU4yQ5DxcsC_wCJzyLxCezdQsz9YcXvcUapPtdoshrpBvrgaWAF703n9xjTTT0wXnXWRuI4SWSsN-6Q1F_40oRy1iQ9JevCD1zz-u2R0Fbd-P8xL8WZ0YhbbAAilSRJzaCgqL8sL57P1BQfIZRAUD6Wtj1GkpLKnhXJUQYpqLI0PJbctS4x8mwEAdnLQ-h9pdIujlQ7t92SL1RyAb2TM_z5tMHQ0rVxALaPZzFcdKiHEbR3zrzSiLbIX3qFYKGzEAoffQ02BLyP9MTnm_qrbq4WsL3fbeOuMstV-NdzZuCHATryda3N3Bi14ha5Bme3Si52iHLCef5UrBxLhwWjFtLTpcjaVqwGKP_pmiw7UsDob3WYcTob3vYs3vMuL59saDvFss4iYMyH3mnMdb_oqhjAtwQIr_jPl_DRcH4ebnsA-TGtqtXpSbdQdXmt0jDAZQrmZtBCYDnNaM3E5ZMcel_-yj3jUcXMOMqPghF1K-uwkLX_q78CoPNSD3OHTxmIw=w800-h400-no)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "numbers_1D = np.array([1, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 5, 6])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numbers_1D + 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Như chúng ta thấy ở bên trên, 3 được **cộng với tất cả (broadcasting)** phần tử của x. \n",
    "\n",
    "Một cách liên tưởng đơn giản giống dòng đầu tiên hình 2, Numpy đã chuyển số 3 thành một array một chiều là [3, 3, 3] để thực hiện phép cộng này."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 5, 6])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Tương đương với\n",
    "numbers_1D + np.array([3, 3, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-6, -5, -4])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Tương tự với phép trừ\n",
    "numbers_1D + 3 - 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### b. Mảng hai chiều"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "numbers_2D = np.asarray([\n",
    "    [4, 5, 6], \n",
    "    [7, 8, 9]\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[24, 25, 26],\n",
       "       [27, 28, 29]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numbers_2D + 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 2. Phép nhân mảng với số"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### a. Mảng 1 chiều"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://lh3.googleusercontent.com/M8bj4OE6-5UwJehuOWcapO9sGg1iNq8jSFNPCDDCPjzIUcytIv3M_KPxlhyqXFIA_5h5gGJXGCE43LeRGOcP56wFF10Xwwu9KgMMxqG8gWIspAl2XQaK-4xudfcHMk2xXCUlXclLanEdiZbbxXavw8_VwtpLWjavHnRGji6DKnC3tJuR80FqduyIR8OfZi6dD7IQSO3H96Bdy8tHhRh_4PtRITQS8hBdnai7l2FZKiyKe-M_vBvTXTlN492pDDmbeTAbE4kemGyefRkP4gJ4p4dQYShXQGa1U_GQaHJFU9JZxK5P9GZg-LF27A1XLflsp13Xi7lSuzNJ5McqoBKt9uZLhAYwAEup7I8DIvFalF2VzkkXf3W6m0HsErRecsG3yz4CGQwYgJvxgxL8OSmPIp8qIcHUIKJzmuET4648f262UdzyN-tia8GH7lTNwMtL5QKnsMjwlkPpLM3YVzOYrLQmAiSOD2PaQLO6TyN86JhIwmw7ES5fgj1EB8XkKv74szENrUwJ56G4KF71bE0nWe1LHOEWDoeZLyZw8jQvpxBlRELoH3M-7QWVBeT_dDgrdR2QwCQc0oGuAFbdj_xshAgXkkTsrrFlGvvBnyPt60kkE_27MWJ3TnsOUquL1vf5H_cWA1O_VTzAViTpHxSjY2a5fvKVPzg_Ew=w800-h400-no)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "numbers_1D_range = np.arange(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numbers_1D_range"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0,  20,  40,  60,  80, 100, 120, 140, 160, 180])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numbers_1D_range * 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### b. Mảng 2 chiều"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "numbers_2D_ones = np.ones((10, 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numbers_2D_ones"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8., 8., 8., 8., 8., 8., 8., 8., 8., 8.],\n",
       "       [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.],\n",
       "       [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.],\n",
       "       [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.],\n",
       "       [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.],\n",
       "       [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.],\n",
       "       [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.],\n",
       "       [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.],\n",
       "       [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.],\n",
       "       [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numbers_2D_ones * 8"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### II. Lan truyền mảng với mảng"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 1. Phép cộng 2 mảng khác chiều"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### a. Mảng 2 chiều với mảng 1 chiều"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://lh3.googleusercontent.com/FsWe2AninhL2yEWjCaHxnHlKbfwuiRPsc-_u0w1JUYVeF7OwDiPj74A404EqDyngpauoG5TI5Q5aV4IdUNthx37hgFZsE2FOoOg4l7igr2Z98jc5NdigINPs5_9YTeUSA_GXoFn3bPawQQChR8kHfaGpp92FjNlrJnQw6lGP9SxsjVzzHztzgTRwCvApK35ikRaze0OyXP8aqMl9pflGafp4WjkgFlApj4eJ3PVWby_D6skeOecCLJXQ7E9R4HaFP-Wv3llCdVfTDf5_s-z8uWnn6T1Hy8nuEPOaoUzw3Gbsi7-9OWfTgDMCTqHYxfCa0ta-NlRurmqMrHn8nh5phMJbyq6lAQSzkn7UC-9EJ9tv0c4QgiBOgZJX9g9chZNR63CeAQSvVVTMqnj5lF3B-tpXKjWAywgQwoNdSM3BztnFJN8-i6MHLJnnUZPnatNbbZHmKkcrBZZQzYqN6kZuw8qx-W3NwTydULrJGJYPbWSAHHYxLaWcvti10YQtqgWcFu4r2jJY_FGIOW50LXimAajbrUQ0F5uVQ00RXJk1AMWHcUtJrj54bP5SDodlrdEkgydOG6cosdK0XJM4ZSgjc9yWjaX-gL_TpjOF8daq6e7AOTyGyz7dChKPuXDNOvwBpWUHxdlS4DmDlg4rlGgP7bGWjo_vvS3lQA=w800-h400-no)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([\n",
    "    [6, 9, 10],\n",
    "    [15, 18, 20],\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = np.array([\n",
    "    [2],\n",
    "    [4],\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 8, 11, 12],\n",
       "       [19, 22, 24]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x + y"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
